package xin.huangyan.accessibilityapp;

import android.accessibilityservice.AccessibilityService;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

import xin.huangyan.accessibilityapp.utils.HLog;

public class AccessibilityUtil {
    public static void openNotification(AccessibilityEvent event){
        HLog.i("openNotification");
        //打开通知栏
        if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
            final PendingIntent contentIntent = ((Notification) event.getParcelableData()).contentIntent;
            try {
                contentIntent.send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
    }
    public static void openMsgDialog(Context context, String msg){
        Intent intent = new Intent(context, WeixinDialogActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("msg", msg);
        context.startActivity(intent);
    }

    public static void startApp(Context context, String packageName) {
        try {
            Intent intent = getAppOpenIntentByPackageName(context, packageName);
            if (intent != null) {
                context.startActivity(intent);
            }
        } catch (Exception e) {

        }
    }
    public static void startWeixinHome(Context context){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
        context.startActivity(intent);
    }
    public static Intent getAppOpenIntentByPackageName(Context context, String packageName){
        String mainAct = null;
        PackageManager pkgMag = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
//        intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        @SuppressLint("WrongConstant") List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
                PackageManager.GET_ACTIVITIES);
        for (int i = 0; i < list.size(); i++) {
            ResolveInfo info = list.get(i);
            if (info.activityInfo.packageName.equals(packageName)) {
                mainAct = info.activityInfo.name;
                break;
            }
        }
        if (TextUtils.isEmpty(mainAct)) {
            return null;
        }
        intent.setComponent(new ComponentName(packageName, mainAct));
        return intent;
    }
}
